import dayjs from 'dayjs';

export function dateFormat(time) {
  if (!time) return '';
  if (('' + time).indexOf('-') > 0 || ('' + time).indexOf('/') > 0) {
    return time.substring(0, 10);
  } else if (('' + time).length === 10) {
    time = parseInt(time) * 1000;
  }
  const d = new Date(time);
  const year = d.getFullYear();
  const month = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');
  return `${year}-${month}-${day}`;
}
export function dateFormatMMdd(time) {
  if (!time) return '';
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000;
  } else if (('' + time).indexOf('-') > 0 || ('' + time).indexOf('/') > 0) {
    return time.substring(0, 10);
  }
  const d = new Date(time);
  const month = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');
  return `${month}-${day}`;
}
export function dateTimesFormat(time, format = null) {
  if (!time) return '-';
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000;
  } else if (('' + time).indexOf('-') > 0 || ('' + time).indexOf('/') > 0) {
    return time;
  }
  const d = new Date(time);
  if (format) {
    return dayjs(time).format(format);
  } else {
    const year = d.getFullYear();
    const month = (d.getMonth() + 1).toString().padStart(2, '0');
    const day = d.getDate().toString().padStart(2, '0');
    const hours = d.getHours().toString().padStart(2, '0');
    const minutes = d.getMinutes().toString().padStart(2, '0');
    const seconds = d.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
}
export function dateTimesWithoutYearFormat(time) {
  if (!time) return '-';
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000;
  } else if (('' + time).indexOf('-') > 0 || ('' + time).indexOf('/') > 0) {
    if (time.length >= 19) {
      return time.substring(5, time.length);
    }
    return time;
  }
  const d = new Date(time);
  const month = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');
  const hours = d.getHours().toString().padStart(2, '0');
  const minutes = d.getMinutes().toString().padStart(2, '0');
  const seconds = d.getSeconds().toString().padStart(2, '0');
  return `${month}-${day} ${hours}:${minutes}:${seconds}`;
}
export function dateMonthFormat(time) {
  if (!time) return '-';
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000;
  } else if (('' + time).indexOf('-') > 0 || ('' + time).indexOf('/') > 0) {
    return time;
  }
  const d = new Date(time);
  const year = d.getFullYear();
  const month = (d.getMonth() + 1).toString().padStart(2, '0');
  return `${year}-${month}`;
}
export function dateYearFormat(time) {
  if (!time) return '-';
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000;
  } else if (('' + time).indexOf('-') > 0 || ('' + time).indexOf('/') > 0) {
    return time;
  }
  const d = new Date(time);
  return d.getFullYear().toString();
}
